package io.renren.modules.user.controller;

import java.util.Arrays;
import java.util.Map;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.user.entity.UserinfoEntity;
import io.renren.modules.user.service.UserinfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.annotation.Resource;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-07-21 13:14:29
 */
@RestController
@RequestMapping("user/userinfo")
public class UserinfoController {
    @Autowired
    private UserinfoService userinfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("user:userinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        System.out.println(params);
        // 通过调用mybatisplus封装好的dao层实现
        // PageUtils page = userinfoService.queryPage(params);


        // 分页
        int pageIndex = Integer.parseInt(String.valueOf(params.get("page")));
        int pageSize = Integer.parseInt(String.valueOf(params.get("limit")));
        String username = String.valueOf(params.get("key"));
        Page<UserinfoEntity> page = new Page<>(pageIndex,pageSize);

        // 通过调用自定义dao层实现
        userinfoService.selectByUser(username,page);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{userNo}")
    @RequiresPermissions("user:userinfo:info")
    public R info(@PathVariable("userNo") Integer userNo){
        //UserinfoEntity userinfo = userinfoService.getById(userNo);

        // UserinfoEntity userinfo = new UserinfoEntity();

        UserinfoEntity userinfo = new UserinfoEntity();
        userinfoService.selectUserById(userNo);
        return R.ok().put("userinfo", userinfo);
    }

//    @RequestMapping("/info/{userNo}")
//    public UserinfoEntity userInfoById(@PathVariable(value = "userNo")Integer userNo){
//        return userinfoService.selectUserById(userNo);
//    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("user:userinfo:save")
    public R save(@RequestBody UserinfoEntity userinfo){
		// userinfoService.save(userinfo);

        userinfoService.insertUser(userinfo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("user:userinfo:update")
    public R update(@RequestBody UserinfoEntity userinfo){
		userinfoService.updateById(userinfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("user:userinfo:delete")
    public R delete(@RequestBody Integer[] userNos){
		userinfoService.removeByIds(Arrays.asList(userNos));

        return R.ok();
    }

}
